package cn.bdqn.controller;

import cn.bdqn.dto.Dto;
import cn.bdqn.mapper.ItripHotelMapper;
import cn.bdqn.mapper.ItripHotelRoomMapper;
import cn.bdqn.pojo.ItripHotel;
import cn.bdqn.pojo.ItripHotelOrder;
import cn.bdqn.pojo.ItripHotelRoom;
import cn.bdqn.pojo.ItripUser;
import cn.bdqn.service.ItripHotelTempStoreService;
import cn.bdqn.service.ItripOrderService;
import cn.bdqn.service.OrderSearchService;
import cn.bdqn.util.VerificationToken;
import cn.bdqn.vo.*;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(description = "订单api")
@Controller
@RequestMapping("/api/hotelorder")
public class ItripOrderController {
@Resource
private OrderSearchService orderSearchService;
    @Resource
    private ItripOrderService itripOrderService;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private ItripHotelMapper itripHotelMapper;
    @Resource
    private ItripHotelRoomMapper itripHotelRoomMapper;
    @Resource
    private ItripHotelTempStoreService itripHotelTempStoreService;

    @RequestMapping(value = "/getpreorderinfo", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto getpreorderinfo(@RequestBody ValidateRoomStoreVO validateRoomStoreVO, HttpServletRequest request){
        Dto dto=new Dto();
        String  token=request.getHeader("token");
        String userAgent=request.getHeader("user-agent");
        ItripUser itripUser= JSON.parseObject((String) redisTemplate.opsForValue().get(token),ItripUser.class);
        if(VerificationToken.verification(userAgent,token)==false||itripUser==null){
            dto.setCode("10086");;
            dto.setMsg("token失效，请重新登录");
            dto.setSuccess("false");
            return dto;
        }
        ItripHotel hotel=itripHotelMapper.getItripHotelById(validateRoomStoreVO.getHotelId());
        ItripHotelRoom room=itripHotelRoomMapper.selectByPrimaryKey(validateRoomStoreVO.getRoomId());
        Map param=new HashMap();
        param.put("startTime", validateRoomStoreVO.getCheckInDate());
        param.put("endTime", validateRoomStoreVO.getCheckOutDate());
        param.put("roomId", validateRoomStoreVO.getRoomId());
        param.put("hotelId", validateRoomStoreVO.getHotelId());
        RoomStoreVO roomStoreVO=new RoomStoreVO();
        roomStoreVO.setCheckInDate(validateRoomStoreVO.getCheckInDate());
        roomStoreVO.setCheckOutDate(validateRoomStoreVO.getCheckOutDate());
        roomStoreVO.setHotelName(hotel.getHotelName());
        roomStoreVO.setRoomId(room.getId());
        roomStoreVO.setPrice(room.getRoomPrice());
        roomStoreVO.setHotelId(validateRoomStoreVO.getHotelId());
        try {
            List<StoreVO> storeVOList = itripHotelTempStoreService.queryRoomStore(param);
            roomStoreVO.setCount(1);
            roomStoreVO.setStore(storeVOList.get(0).getStore());
            dto.setData(roomStoreVO);
            dto.setMsg("查询剩余房间成功");
            return dto;

        } catch (Exception e) {
            e.printStackTrace();
            dto.setCode("10086");
            dto.setMsg("系统异常");
            return dto;
        }
    }





    /**
     *根据订单ID查看个人订单详情-房型相关信息
     * @param orderId
     * @return
     */
    @ResponseBody
    @ApiOperation(value = "根据订单ID查看个人订单详情-房型相关信息", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "根据订单ID查看个人订单详情-房型相关信息")
    @RequestMapping(value = "/getpersonalorderroominfo/{orderId}", method = RequestMethod.GET, produces = "application/json")
    public Dto getpersonalorderroominfo(@ApiParam(required = true, name = "orderId", value = "订单ID")
                                        @PathVariable Long orderId,
                                        HttpServletRequest request){
        Dto dto=new Dto();
        boolean loginVerify=new VerificationToken().verification(request.getHeader("user-agent"),request.getHeader("token"));
        if (loginVerify){
            if (orderId!=null&&orderId>0){
                ItripPersonalOrderRoomVO itripPersonalOrderRoomVO=itripOrderService.getpersonalorderroominfo(orderId);
                if (itripPersonalOrderRoomVO!=null&&!itripPersonalOrderRoomVO.equals("")){
                    dto.setCode("0");
                    dto.setMsg("查询成功");
                    dto.setSuccess("true");
                    dto.setData(itripPersonalOrderRoomVO);
                    return dto;
                }else {
                    dto.setCode("10086");
                    dto.setCode("查询结果为空");
                    return dto;
                }
            }else {
                dto.setCode("10086");
                dto.setCode("传入参数为空");
                dto.setSuccess("false");
                return dto;
            }
        }else {
            dto.setCode("10086");
            dto.setCode("账号未登录");
            dto.setSuccess("false");
            return dto;
        }

    }


    /**
     *根据订单ID查看订单信息
     * @param orderId
     * @return
     */
    @ResponseBody
    @ApiOperation(value = "根据订单ID查看订单信息", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "根据订单ID查看订单详情")
    @RequestMapping(value = "/queryOrderById/{orderId}", method = RequestMethod.GET, produces = "application/json")
    public Dto selectOrderMessage(@PathVariable Long orderId,HttpServletRequest request){
        Dto dto=new Dto();
        ItripHotelOrder itripHotelOrder=orderSearchService.getByPrimaryKey(orderId);
        if(itripHotelOrder!=null){
            dto.setCode("10001");
            dto.setMsg("查询不到订单");
        }
        ItripOrderModifyVO itripOrderModifyVO=new ItripOrderModifyVO();
        itripOrderModifyVO.setId(itripHotelOrder.getId());
        itripOrderModifyVO.setPayType(itripHotelOrder.getPayType());
        itripOrderModifyVO.setOrderType(itripHotelOrder.getOrderType());
        itripOrderModifyVO.setOrderNo(itripHotelOrder.getOrderNo());
        itripOrderModifyVO.setHotelId(itripHotelOrder.getHotelId());
        itripOrderModifyVO.setHotelName(itripHotelOrder.getHotelName());
        itripOrderModifyVO.setRoomId(itripHotelOrder.getRoomId());
        itripOrderModifyVO.setCount(itripHotelOrder.getCount());
        itripOrderModifyVO.setBookingDays(itripHotelOrder.getBookingDays());
        itripOrderModifyVO.setCheckInDate(itripHotelOrder.getCheckInDate());
        itripOrderModifyVO.setCheckOutDate(itripHotelOrder.getCheckOutDate());
        itripOrderModifyVO.setNoticePhone(itripHotelOrder.getNoticePhone());
        itripOrderModifyVO.setNoticeEmail(itripHotelOrder.getNoticeEmail());
        itripOrderModifyVO.setSpecialRequirement(itripHotelOrder.getSpecialRequirement());
        itripOrderModifyVO.setIsNeedInvoice(itripHotelOrder.getIsNeedInvoice());
        itripOrderModifyVO.setInvoiceType(itripHotelOrder.getInvoiceType());
        itripOrderModifyVO.setInvoiceHead(itripHotelOrder.getInvoiceHead());
        itripOrderModifyVO.setBookType(itripHotelOrder.getBookType());
        itripOrderModifyVO.setLinkUserName(itripHotelOrder.getLinkUserName());
        Long id=itripHotelOrder.getId();
        List<ItripLinkUserMessageVO> list=itripOrderService.selectOrderLinkUserMessageByOrderId(id);
        dto.setMsg("订单信息");
        itripOrderModifyVO.setItripOrderLinkUserList(list);
        dto.setData(itripOrderModifyVO);
        return dto;
    }


    //刷新订单,每10分钟刷新未支付的订单
    @Scheduled(cron = "*0 0/10 * * * ?")
    public void flushCancelOrderStatus() {
        try {
            boolean flag = itripOrderService.flushOrderStatus(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //刷新订单,每2小时刷新未点评订单
    @Scheduled(cron = "0 0 0/2 * * ?")
    public void flushSuccessOrder() {
        try {
            boolean flag = itripOrderService.flushOrderStatus(2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
